import request from '../utils/request'
import router from '../router'

// 购物车下单
export function requestShopCartOrder (addressid) {
    return new Promise(resolve => {
        request.post('/order/addchecked', {
            userid: localStorage.getItem('userid'),
            addressid
        }, {}).then(res => {
            resolve(res.data)
        })
    })
}

// 获取购物车下单列表
export function requestShopCartlist () {
    return new Promise(resolve => {
        request.get('/order/checklist', {
            params: {
                userid: localStorage.getItem('userid')
            }
        }).then(res => {
            resolve(res.data)
        })
    })
}

// 单一下单
export function requestOrder (params) {
    return new Promise(resolve => {
        request.post('/order/add', Object.assign(params, {
            userid: localStorage.getItem('userid')
        }), {
            headers: {
                loading: false
            }
        }).then(res => {
            if (res.data.orderid === '添加失败') {
                res.data.msg = '下单失败，请再试一次'
                router.back()
                resolve(res.data)
                return
            }
            resolve(res.data)
        })
    })
}

// 获取用户不同状态下订单列表
export function requestGetOrderByState (state) {
    return new Promise(resolve => {
        request.get('/order/statelist', {
            params: {
                userid: localStorage.getItem('userid') || '',
                state
            }
        }).then(res => {
            resolve(res.data)
        })
    })
}

// 获取订单详情信息
export function requestOrderDetail (orderid) {
    return new Promise(resolve => {
        request.get('/order/detail', {
            params: {
                orderid
            }
        }).then(res => {
            resolve(res.data)
        })
    })
}

// 确认收货
export function requestGetIt (orderid) {
    return new Promise(resolve => {
        request.put('/order/confirm', {
            orderid
        }).then(res => {
            resolve(res.data)
        })
    })
}

// 退货申请
export function requestReturn (orderid) {
    return new Promise(resolve => {
        request.put('/order/back', {
            orderid
        }).then(res => {
            resolve(res.data)
        })
    })
}
